//
//  GYDMessageViewModel.swift
//  Weibo
//
//  Created by 贵永冬 on 16/7/16.
//  Copyright © 2016年 贵永冬. All rights reserved.
//

import UIKit

class GYDMessageViewModel: NSObject {
    weak private var messageVC : GYDMessageViewController?
    
    init(messageVC : GYDMessageViewController) {
        super.init()
        self.messageVC = messageVC
    }
    func startService() -> Void {
        let isLogin = GYDUserModel.sharedInstance.isLogin
        if isLogin {
            self.hiddenVisitorView()
        }else {
            self.showVisitorView()
        }
        GYDUserModel.sharedInstance.loginResultSignal.subscribeNext { [unowned self] (x) in
            if x as! String == LOGIN_SUCCESS {
                self.hiddenVisitorView()
            }else if x as! String == LOGINOUT_SUCCESS {
                self.showVisitorView()
            }
        }
    }
    private func showVisitorView() -> Void {
        
        let subViews = self.messageVC?.view.subviews
        for view in subViews! {
            view.removeFromSuperview()
        }
        let visitorView = GYDVisitorView(frame: self.messageVC!.view.bounds, visitorType: VisitorType.Message)
        visitorView.tag = 888
        self.messageVC?.view.addSubview(visitorView)
        visitorView.registSignal.subscribeNext { [unowned self] (x) in
            let loginVC = GYDLoginViewController()
            self.messageVC?.presentViewController(loginVC, animated: true, completion: nil)
        }
        visitorView.loginSignal.subscribeNext { [unowned self] (x) in
            let loginVC = GYDLoginViewController()
            self.messageVC?.presentViewController(loginVC, animated: true, completion: nil)
        }
    }
    private func hiddenVisitorView() -> Void {
        let subViews = self.messageVC?.view.subviews
        for view in subViews! {
            view.removeFromSuperview()
        }
        let findGroupBarButtonItem = UIBarButtonItem(title: "发现群", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(GYDMessageViewModel.findGroupButtonClick))
        self.messageVC?.navigationItem.leftBarButtonItem = findGroupBarButtonItem
        let messageBarItem = UIBarButtonItem(image: UIImage(named: "navigationbar_icon_newchat"), style: UIBarButtonItemStyle.Plain, target: self, action: #selector(GYDMessageViewModel.messageButtonClick))
        self.messageVC?.navigationItem.rightBarButtonItem = messageBarItem
    }
    
    @objc private func messageButtonClick() -> Void {
        let popVC = GYDPopViewController(frame:CGRect(x: GYDWindowWidth - 150 - 8, y: 55, width: 150, height: 100))
        popVC.popType = PopType.PopRight
        popVC.dataSoureType = PopDataSoureType.TitleAndImage
        popVC.dataSoure = [["imageName":"popover_icon_newchat","title":"发起聊天"],["imageName":"popover_icon_privatechat","title":"私密聊天"]]
        self.messageVC?.presentViewController(popVC, animated: true, completion: nil)
    }
    @objc private func findGroupButtonClick() -> Void {
        
    }
}
